#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <signal.h> 
#include <stdlib.h>       
#include <string.h>
#include <pthread.h>
//#include <sys/sem.h>
#include <errno.h>
#include <sys/ipc.h>
//#include <sys/msg.h>
#include <sys/timeb.h>
#include <sys/socket.h>
//#include <sys/socketvar.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <semaphore.h>
#include <string.h>
#include <termios.h>
#include <linux/input.h>
#include <semaphore.h>
#include <linux/shm.h>
#include <asm/unistd.h>
#include <sys/poll.h>

#include "../common.h"

//protoSlaveType
enum _protoSlaveType
{
		_setUsbHost							= 0x00,
		_updateTerminalPrograme	= 0x01,
		_rebootSystem						= 0x02,
};

static int sock = -1;
static struct sockaddr_in addr;	

static void setUsbHost(struct recvData *recvData, struct packageFilter *pFilter);
static void updateTerminalPrograme(struct recvData *recvData, struct packageFilter *pFilter);
static void rebootSystem(struct recvData *recvData, struct packageFilter *pFilter);

void app2CloudServer(struct recvData *recvData)
{
		printf("into app2CloudServer function\n");	
}

void app2Cloud(struct recvData *recvData)
{
		printf("into app2Cloud\n");
		
		if( sock == -1 )
   	{
		   	sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);		
				addr.sin_family     = AF_INET;
				addr.sin_addr.s_addr = inet_addr( "127.0.0.1" );
				addr.sin_port       = htons( 9400 );
				
				if( sock == -1 ) return;
		}
		
		if(recvData == NULL)
		{
				printf("recvData == NULL ! {%s(%d)}\n",__FILE__,__LINE__);
				return;	
		}
		
		struct packageFilter filter;
		int ret = explainPackageHead(recvData, &filter);
		if(ret < 0)
		{
				return;	
		}
		
		switch(filter.protoSlaveType)
		{
				case _setUsbHost:
				{
						setUsbHost(recvData, &filter);
				}
				break;
				case _updateTerminalPrograme:
				{
						updateTerminalPrograme(recvData, &filter);
				}
				break;
				case _rebootSystem:
				{
						rebootSystem(recvData, &filter);
				}
				break;
				default:
					char logBuf[128];
					sprintf(logBuf, "app2CloudServer: unknow protoSlaveType ! protoSlaveType = %d\n", filter.protoSlaveType);
					writeLog(logBuf, __FILE__, __LINE__);
					break;
		}
}


static void setUsbHost(struct recvData *recvData, struct packageFilter *pFilter)
{
		unsigned char *revData = recvData->buf;
		
		//exec
		char *command = (char*)(pFilter->pData);
		if(command == NULL)
		{
				printf("command == NULL ! {%s(%d)}\n",__FILE__,__LINE__);
				return;
		}
		
		revData[recvData->count] = '\0';
		printf("setUsbHost:command = %s\n", command);
		system(command);		
}

static void updateTerminalPrograme(struct recvData *recvData, struct packageFilter *pFilter)
{
		realUpdatePrograme();
		//realUpdateProgrameEx();
}

static void rebootSystem(struct recvData *recvData, struct packageFilter *pFilter)
{
	
}